<template>
	<view class="content">
		<view class="top-bar">
			<view class="top-item" v-for="(item,index) in topList" :key="index"
				:class="{current:tabCurrentIndex === index}" @click="tabClick(index)">
				{{item.text}}
			</view>
		</view>

		<swiper duration="300" class="swiper-box" :current="tabCurrentIndex" @change="tabChange">
			<swiper-item class="swiper-item" v-for="(tabItem,tabIndex) in topList" :key="tabIndex">
				<scroll-view scroll-y="true" class="list-content" @scrolltolower="loadData" refresher-enabled="true"
					:refresher-triggered="triggered" @refresherpulling="onPullingdown" @refresherrefresh="onRefresh"
					@refresherrestore="onRestore" @refresherabort="onAbort">

					<!-- <refresh class="load-more" @refresh="onRefresh" @pullingdown="onPullingdown"
						:display="refreshing ? 'show' : 'hide'">
						<loading-indicator class='indicator'></loading-indicator>
						<text class="load-more-text">{{refreshText}}</text>
					</refresh> -->

					<view class="order-item" v-for="(orderItem,orderIndex) in tabItem.orderList" :key="orderIndex"
						@click="goDetail(orderItem)">
						<view class="top">
							<uni-icons type="shop"></uni-icons>
							<text class="store-name">{{orderItem.storeName}} ></text>
							<view class="order-state">{{orderItem.orderStatus}}</view>
						</view>

						<view class="goods">
							<image :src="orderItem.orderGoods[0].fullPath"></image>
							<view class="right">
								<view class="goods-name">{{orderItem.orderGoods[0].name}}</view>
								<view class="goods-price">¥{{orderItem.orderGoods[0].goodsPrice}}</view>
								<view class="goods-number">
									x{{orderItem.orderGoods[0].goodsCount > 0 ? orderItem.orderGoods[0].goodsCount : 1}}
								</view>
							</view>

						</view>

						<view class="goods-total">
							共{{orderItem.orderGoods[0].goodsCount > 0 ? orderItem.orderGoods[0].goodsCount : 0}}件商品，合计：<text
								class="total-price">¥{{orderItem.orderGoods[0].goodsPrice}}</text> (含运费¥0)
						</view>

						<view class="action-box">
							<button class="action-btn" v-if="orderItem.showOrderCancelApply || orderItem.waitPay"
								@click.stop="cancelOrder(orderItem)">取消订单</button>
							<button class="action-btn" v-if="orderItem.delete"
								@click.stop="deleteOrder(orderItem)">删除订单</button>
							<button class="action-btn recom" v-if="orderItem.waitPay"
								@click.stop="gotoPay(orderItem)">立即支付</button>
						</view>
					</view>

					<view class="uni-data-loading">
						<uni-load-more :status="tabItem.loadingType" iconType="auto" :iconSize="16"
							:content-text="contentText">
						</uni-load-more>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				pageIndex: 1,
				tabCurrentIndex: 0,
				refreshing: false,
				loading: true,
				triggered: false,
				refreshText: '下拉刷新',
				loadText: '加载中',
				currentState: '',
				topList: [{
						state: 'all',
						text: '全部',
						loadingType: 'more',
						orderList: []
					},
					{
						state: '10',
						text: '待付款',
						loadingType: 'more',
						orderList: []
					},
					{
						state: '20',
						text: '待发货',
						loadingType: 'more',
						orderList: []
					},
					{
						state: '30',
						text: '待收货',
						loadingType: 'more',
						orderList: []
					},
					{
						state: '40',
						text: '待评价',
						loadingType: 'more',
						orderList: []
					}
				],
				contentText: {
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},
		onLoad(option) {
			this.tabCurrentIndex = option.state ? option.state : 0;
			console.log("this.tabCurrentIndex: ", this.tabCurrentIndex);

			if (this.tabCurrentIndex == 0) {
				this.loadData();
			}
		},
		methods: {
			async loadData(option) {

				let topItem = this.topList[this.tabCurrentIndex];
				this.currentState = topItem.state;

				//tab切换只有第一次需要加载数据
				if (option === 'tabChange' && topItem.loaded === true) {
					return;
				}
				//防止重复加载
				if (topItem.loadingType == 'loading') {
					return;
				}
				//刷新数据
				if (option == 'refresh') {
					this.pageIndex = 1;
					topItem.orderList = [];
				}

				topItem.loadingType = 'loading';

				let params = {
					"orderStatus": this.currentState
				}

				let data = await this.helper.requestAPI(this.api.orderlist, params, this.pageIndex);
				console.log("获取data: ",data);
				let orderList = data.body.orderList;
				if (orderList.length == 0) {
					topItem.loadingType = 'noMore'
					return;
				}

				topItem.orderList = topItem.orderList.concat(orderList);
				topItem.loadingType = 'more';

				this.$set(topItem, 'loaded', true);
				this.pageIndex++;
			},
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			tabChange(e) {
				this.tabCurrentIndex = e.target.current;
				this.pageIndex = 1;
				this.loadData('tabChange');
			},
		    deleteOrder(item) {

				uni.showModal({
					title: '提示',
					content: '确认删除订单吗？',
				    success: async (res) => {
						if (res.confirm) {
							let data = {
								"orderStatus": this.currentState,
								"orderId": item.orderId,
								"type": "delete",
								"orderType": this.currentState
							}
							
						    let res = await this.helper.requestAPI(this.api.orderdelete,data,1);
							this.onRefresh();
							this.$api.msg('删除成功');
						}
					}
				})
			},
			cancelOrder(item) {
				uni.showModal({
					title: '提示',
					content: '确认取消订单吗？',
					success: res => {
						if (res.confirm) {
							this.$api.msg('取消成功');
						}
					}
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: './orderDetail?orderId=' + item.orderId
				});
			},
			async gotoPay(item) {
				uni.showLoading({
					title: '跳转支付中...',
					mask: true
				});
				
				let data = {
					"orderId": item.outOrderId
				}
				
				let res = await this.helper.requestAPI(this.api.orderpay,data);
				let orderInfo = res.body;
				uni.navigateTo({
					url: '../payment/payment?orderDto=' + encodeURIComponent(JSON
						.stringify(orderInfo.orderDto))
				})
			},
			onRefresh: function(event) {
				console.log(event);

				this.refreshing = true;
				this.refreshText = "加载中";
				this.loadData('refresh');

				// setTimeout(() => {
				// 	this.refreshing = false;
				// 	this.refreshText = "下拉刷新";
				// }, 1500)

				setTimeout(() => {
					this.triggered = false;
					this.loading = false;
				}, 1000)
			},
			onPullingdown: function(event) {
				// var distance = event.pullingDistance;
				// if (distance >= 30) {
				// 	this.refreshText = "松开立即刷新";
				// } else {
				// 	this.refreshText = "下拉刷新";
				// }

				this.triggered = true;

			},
			onAbort() {
				this.triggered = false;
			},
			onRestore() {
				this.triggered = 'restore';
			}
		}
	}
</script>

<style lang="scss">
	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.top-bar {
		display: flex;
		position: relative;
		background-color: white;
		height: 40px;
		padding: 0 5px;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		z-index: 10;

		.top-item {
			display: flex;
			position: relative;
			justify-content: center;
			align-items: center;
			flex: 1;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}


	}

	.swiper-box {
		height: calc(100% - 40px);
	}

	.list-content {
		height: 100%;
	}

	.load-more {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 750rpx;
		height: 80rpx;
	}

	.load-more-text {
		line-height: 80rpx;
		font-size: 28rpx;
		text-align: center;
		margin-left: 30rpx;
	}

	.indicator {
		color: gray;
	}

	.uni-data-loading {
		display: flex;
		justify-content: center;
		width: 100%;
		color: #999;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		margin: 20rpx;
		padding: 20rpx 15rpx;
		border-radius: 15rpx;
		background-color: white;

		.top {
			display: flex;
			align-items: center;

			.store-name {
				font-size: $uni-font-size-sm;
				margin-left: 20rpx;
			}

			.order-state {
				flex: 1;
				font-size: $uni-font-size-sm;
				color: $base-color;
				text-align: right;
			}
		}

		.goods {
			display: flex;
			margin-top: 30rpx;

			.right {
				padding: 0 20rpx;
				height: 150rpx;
				position: relative;
				flex: 1;

				.goods-name {
					font-size: $uni-font-size-sm;
					padding-right: 100rpx;
				}

				.goods-price {
					position: absolute;
					font-size: $uni-font-size-sm + 2rpx;
					color: $font-color-dark;
					top: 0;
					right: 0;
				}

				.goods-number {
					position: absolute;
					font-size: $uni-font-size-sm;
					color: $uni-text-color-grey;

					right: 0;
					bottom: 0;
				}
			}

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}

		.goods-total {
			margin-top: 20rpx;
			text-align: right;
			color: $font-color-light;
			font-size: $uni-font-size-sm;

			.total-price {
				color: $font-color-dark;
				margin-right: 10rpx;
			}
		}

		.action-box {
			display: flex;
			justify-content: flex-end;
			margin-top: 30rpx;

			.action-btn {
				width: 160rpx;
				height: 60rpx;
				margin: 0;
				margin-left: 24rpx;
				padding: 0;
				text-align: center;
				line-height: 60rpx;
				font-size: $uni-font-size-sm + 1rpx;
				color: $font-color-dark;
				background: #fff;
				border-radius: 100rpx;

				&:after {
					border-radius: 100px;
				}

				&.recom {
					background: #fff9f9;
					color: $base-color;

					&:after {
						border-color: #f7bcc8;
					}
				}
			}
		}

	}
</style>
